﻿using AutoMapper;
using Daily.Application.DTO;
using DailyAPP.Domain.Model;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Study.API.ApiResponses;

namespace Study.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController()]
    public class LoginController : Controller
    {
        private readonly DailyDbContext dbContext;
        private readonly IMapper mapper;

        public LoginController(DailyDbContext dbContext,IMapper mapper )
        {
            this.dbContext = dbContext;
            this.mapper = mapper;
        }

        /// <summary>
        ///注册
        /// 100:账号已存在 101:账号注册成功 102:服务器错误 103:输入参数无效
        /// </summary>
        /// <param name="inputDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult<ApiResponse<User>>> Registor([FromBody]LoginInputDTO inputDTO)
        {

            try {
                if (!ModelState.IsValid)
                {
                    return BadRequest(new ApiResponse<User>()
                    {
                        Msg = "输入参数无效",
                        ResultCode = 103,
                        Errors = ModelState.Values.SelectMany(v => v.Errors.Select(e => new ErrorDetail() {Field=v.AttemptedValue, Message= e.ErrorMessage })).ToList()
                    });
                }
                if (await dbContext.Users.AnyAsync(t => t.AccountName == inputDTO.AccountName))
                {
                    return Conflict( new ApiResponse<User>() { Msg="账号已存在!",ResultCode=100});
                }
                else {
                    //User user = new User() { UserName=inputDTO.UserName,AccountName=inputDTO.AccountName,Password=inputDTO.Password};
                    User user = mapper.Map<User>(inputDTO);
                    await dbContext.Users.AddAsync(user);
                    int result = await dbContext.SaveChangesAsync();
                    if (result == 1)
                    {
                        return new ApiResponse<User>() { Msg="账号注册成功！",ResultCode=101,Data= user };
                    }
                    else
                        return BadRequest(new ApiResponse<User>() { Msg = "服务器错误！", ResultCode = 102 });

                }
            } catch { 
                        return BadRequest(new ApiResponse<User>() { Msg = "服务器内部错误！", ResultCode = 102 });
            }
        }
        /// <summary>
        /// 登录
        /// 100 登录成功
        /// 102 服务器内部错误
        /// 104 账号或密码错误
        /// </summary>
        /// <param name="account"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult<ApiResponse<User>>> Login(string account,string pwd)
        {
            try
            {
                var user= await dbContext.Users.FirstOrDefaultAsync(t=>t.AccountName== account && t.Password==pwd);
                if (user == null) {
                    return new ApiResponse<User>() { Msg = "账号或密码错误！", ResultCode = 104 };
                }
                return new ApiResponse<User>() { Msg = "登录成功！", ResultCode = 100,Data= user };

            }
            catch
            {
                return BadRequest(new ApiResponse<User>() { Msg = "服务器内部错误！", ResultCode = 102 });
            }
        }
    }
}
